package controllers;

import java.awt.Image;
import java.util.*;
import play.mvc.*;

import models.*;
import play.*;
import play.data.validation.*;
import play.libs.Images;


public class Application extends Controller {

    public static void index() {
        Post frontPost = Post.find("order by PostedAt desc").first();
        List <Post> olderPosts = Post.find(
                "order by postedAt desc"
           ).from(1).fetch(10);
        render(frontPost, olderPosts);
    }
    
    @Before
    static void addDefaults() {
        renderArgs.put("Blog Title",Play.configuration.getProperty("blog.title"));
    }

    public static void show(Long id) {
       Post post = Post.findById(id);
      render(post);
    }

    public static void postComment(Long postId, @Required String author, @Required String content) {
        Post post = Post.findById(postId);
        if (validation.hasErrors()) {
            render("Application/show.html", post);
        }
        post.addComment(author, content);
        flash.success("Thinks for posting %s",author);
        show(postId);
    }

    public static void captcha(){
        Images.Captcha captcha = Images.captcha();
    }
}
